package com.seraph.td.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Iterator;

public class UuseeNIOServer {
	static int TIMEOUT = 300;
	static UuseeNIOServerLinster mLinster;
	//
	static void open(int port){
		ServerSocketChannel mChannel = null;
		Selector mSelector;
		try {
			 mChannel = ServerSocketChannel.open();
			 mSelector = Selector.open();
			 mChannel.configureBlocking(false);
			 mChannel.socket().bind(new InetSocketAddress(port));
			 mChannel.register(mSelector, SelectionKey.OP_ACCEPT
					 					 |SelectionKey.OP_READ
					 					 |SelectionKey.OP_WRITE);
			 /**
			  * wait 4 accpeted
			  */
			 while(true){
				 if(mSelector.select(TIMEOUT)==0){
					mLinster.onAccpeting(); 
				 }else{
					 Iterator<SelectionKey> iterator = mSelector.keys().iterator();
					 if(iterator.hasNext()){
						 SelectionKey key =  iterator.next();
//						 key.
					 }
					
					 
					 break;
				 }
					 
					
			 }
			 /**
			  * connected
			  */
			 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
